# -*- coding: utf-8 -*-

class DummyTagFormatter(object):
    @staticmethod
    def formatSingleTag(tag, strList):
        return tag.unformatted()

    @staticmethod
    def formatTag(tag, strList):
        return tag.unformatted()

class TagFormatter(object):
    _tab = ' '*4

    _singleTagFormat = '%s'
    _singleTagContentFormat = '%s %s'
    
    useLowerCase = False

    @staticmethod
    def singleTagContentFormat(tag, strList):
        return TagFormatter._singleTagContentFormat%tuple(strList)#if strList[1] else strList[0]

    @staticmethod
    def formatSingleTag(tag, strList):
        strList = list(strList)
        if TagFormatter.useLowerCase:
            tname = strList[0].lower()
            strList = [tname, strList[1]]
        sTag = tag.renderTag(TagFormatter.singleTagContentFormat(tag, strList))
        return TagFormatter._singleTagFormat%sTag

    _tagFormat = '%s\n%s%s'
    _contentItemFormat = '%s\n'
    _openedTagContentFormat = '%s %s'
    _closedTagContentFormat = '%s'

    @staticmethod
    def openedTagContentFormat(tag, strList):
        return TagFormatter._openedTagContentFormat%strList if strList[1] else strList[0]

    @staticmethod
    def closedTagContentFormat(tag, s):
        return TagFormatter._closedTagContentFormat%s

    @staticmethod
    def contentItemFormat(tag, cItem):
        return TagFormatter._contentItemFormat%cItem

    @staticmethod
    def contentFormat(tag, content):
#	cont = content
#	if type(content) is not str:
        cont = ''.join(TagFormatter.contentItemFormat(tag, c) for c in content)
        cont = ''.join(TagFormatter._tab+s for s in cont.splitlines(True))
        return cont

    @staticmethod
    def formatTag(tag, strList):
        if TagFormatter.useLowerCase:
            tname1 = strList[0].lower()
            tname2 = strList[3].lower()
            strList = [tname1, strList[1], strList[2], tname2]
        part1 = tag.renderOpenedTag(TagFormatter.openedTagContentFormat(tag, strList[0:2]))
        part2 = TagFormatter.contentFormat(tag, strList[2])
        part3 = tag.renderClosedTag(TagFormatter.closedTagContentFormat(tag, strList[3]))
        return TagFormatter._tagFormat%(part1,part2,part3)
